package com.nsgf.library.view;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

public class SharedPreferencesCallback implements SharedPreferences {

    private SharedPreferences prefs;
    private OnDataChangeListener listener;

    public SharedPreferencesCallback(Context context, String name, int mode, OnDataChangeListener listener) {
        this.prefs = context.getSharedPreferences(name, mode);
        this.listener = listener;
    }

    // 其他实现 SharedPreferences 接口的方法

    @Override
    public Map<String, ?> getAll() {
        return Collections.emptyMap();
    }

    @Nullable
    @Override
    public String getString(String s, @Nullable String s1) {
        return "";
    }

    @Nullable
    @Override
    public Set<String> getStringSet(String s, @Nullable Set<String> set) {
        return Collections.emptySet();
    }

    @Override
    public int getInt(String s, int i) {
        return 0;
    }

    @Override
    public long getLong(String s, long l) {
        return 0;
    }

    @Override
    public float getFloat(String s, float v) {
        return 0;
    }

    @Override
    public boolean getBoolean(String s, boolean b) {
        return false;
    }

    @Override
    public boolean contains(String key) {
        return prefs.contains(key);
    }

    @Override
    public Editor edit() {
        return new EditorCallback(prefs.edit(), listener);
    }

    @Override
    public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {

    }

    @Override
    public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {

    }

    // ... 其他 SharedPreferences 方法的实现

    public interface OnDataChangeListener {
        void onDataChanged();
    }

    private class EditorCallback implements Editor {
        private Editor editor;
        private OnDataChangeListener listener;

        public EditorCallback(Editor editor, OnDataChangeListener listener) {
            this.editor = editor;
            this.listener = listener;
        }

        // 实现 Editor 接口的方法，在数据改变时触发回调

        @Override
        public Editor putString(String s, @Nullable String s1) {
            return null;
        }

        @Override
        public Editor putStringSet(String s, @Nullable Set<String> set) {
            return null;
        }

        @Override
        public Editor putInt(String s, int i) {
            return null;
        }

        @Override
        public Editor putLong(String s, long l) {
            return null;
        }

        @Override
        public Editor putFloat(String s, float v) {
            return null;
        }

        @Override
        public Editor putBoolean(String key, boolean value) {
            editor.putBoolean(key, value);
            if (listener != null) {
                listener.onDataChanged();
            }
            return this;
        }

        @Override
        public Editor remove(String s) {
            return null;
        }

        @Override
        public Editor clear() {
            return null;
        }

        // ... 其他 Editor 方法的实现

        @Override
        public boolean commit() {
            boolean result = editor.commit();
            if (listener != null) {
                listener.onDataChanged();
            }
            return result;
        }

        @Override
        public void apply() {

        }

        // ... 其他 Editor 方法的实现
    }
}
